<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class AccostLogs extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string
     */
    protected $connection = 'stat';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'accost_logs';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id','chat_user_id','expires_time','dt'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'user_id' => 'integer', 'chat_user_id' => 'integer', 'expires_time' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * 添加记录
     * @param $user_id
     * @param $chat_user_id
     * @param $expires_time
     * @return bool
     */
    public static function addRecord($user_id,$chat_user_id,$expires_time)
    {
        self::create([
            'user_id' => $user_id,
            'chat_user_id' => $chat_user_id,
            'expires_time' => $expires_time,
            'dt'=>date('Y-m-d')
        ]);
        return true;
    }

    /**
     * 获取当日搭讪过的用户ID
     * @param int $uid
     * @return array
     */
    public static function getLogByUid($uid=0)
    {
        if(!$uid) return [];
        $dt = time();
        return self::where('user_id',$uid)
            ->where('expires_time','>',$dt)
            ->orderBy('id','desc')
            ->pluck('chat_user_id')->limit(200)->toArray();
    }
}